<template>
  <div>
    <el-dialog :title="showTitle" :visible="dialogVisible" @close="close">
      <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="2~10个字符" size="mini" />
        </el-form-item>

        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" placeholder="2~10个字符" size="mini" />
        </el-form-item>

        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="formData.managerId" placeholder="请选择负责人" size="mini">
            <el-option
              v-for="(item) in deptsHeadUsers "
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" type="textarea" :rows="4" placeholder="1~200个字符" size="mini" />
        </el-form-item>

        <el-form-item>
          <el-row>
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {
  getDepartmentList,
  getDepartmentHeadUserList,
  addDepartment,
  getTreeNodeDetail,
  updateDepartment
} from '@/api/department'
export default {
  name: 'TreeDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    selectNodeId: {
      type: Number,
      default: null
    }

  },
  data() {
    return {
      deptsHeadUsers: [],
      formData: {
        code: '', // 部门编码,
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人名字
        name: '', // 部门名称
        pid: '' // 部门父级部门id

      },
      rules: {
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码长度在 2 到 10 个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let res = await getDepartmentList()
              // 如果formdata的id存在  则说明是编辑  需要将自身排除在外进行校验
              if (this.formData.id) {
                res = res.filter(k => k.id !== this.formData.id)
              }
              if (res.some(item => item.code === value)) {
                callback(new Error('该部门编码已存在'))
              } else {
                callback()
              }
            }
          }
        ], // 部门编码,
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 200, message: '部门介绍长度在 1 到 200 个字符', trigger: 'blur' }
        ], // 部门介绍
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ], // 部门负责人名字
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称长度在 2 到 10 个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let res = await getDepartmentList()
              // 如果formdata的id存在  则说明是编辑  需要将自身排除在外进行校验
              if (this.formData.id) {
                res = res.filter(k => k.id !== this.formData.id)
              }
              if (res.some(item => item.name === value)) {
                callback(new Error('该部门名称已存在'))
              } else {
                callback()
              }
            }
          }
        ] // 部门名称

      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '更新部门' : '新增部门'
    }
  },
  created() {
    this.getDepartmentHeadUserList()
  },
  methods: {
    close() {
      // 重置表单
      this.formData = {
        code: '', // 部门编码,
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人名字
        name: '', // 部门名称
        pid: '' // 部门父级部门id
      }
      this.$refs.formData.resetFields()
      this.$emit('update:dialog-visible', false)
    },
    async getDepartmentHeadUserList() {
      const res = await getDepartmentHeadUserList()
      this.deptsHeadUsers = res
    },
    async btnOk() {
      let msg = '添加'
      try {
        await this.$refs.formData.validate()
        if (this.formData.id) {
          msg = '更新'
          await updateDepartment(this.formData)
        } else {
          await addDepartment({ ...this.formData, pid: this.selectNodeId })
        }

        // 通知父组件更新树组件
        this.$emit('updateTree')
        this.$message.success(`${msg}部门数据成功`)
        this.close()
      // 关闭弹层
      } catch (error) {
        console.dir('校验未通过')
      }
    },
    async getTreeNodeDetail() {
      try {
        const res = await getTreeNodeDetail(this.selectNodeId)
        this.formData = res
        this.$message.success('获取节点详情成功')
      } catch (error) {
        console.log(error)
        this.$message.error('获取节点信息失败')
      }
    }
  }

}
</script>

<style>

</style>
